package session;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import entities.Company;
import entities.Intern;

/**
 * Kullanicinin sisteme girisi ve kayitlanmasiyla ilgili islemlerin yapildigi
 * sinif..
 * 
 * @author gurkan
 */
@Name("userLogin")
@Scope(ScopeType.CONVERSATION)
public class UserLogin {

	private String userName;
	private String password;
	private String role;

	@In
	private EntityManager entityManager;

	@In(value = "userData", create = true)
	private UserData userData;

	/**
	 * Register sayfasindaki h:selectOneMenu bileseni icin kullanici tiplerini
	 * dondurur..
	 * 
	 * @return SelectItem
	 */
	public List<SelectItem> getSelectItems() {
		List<SelectItem> selectItems = new ArrayList<SelectItem>();
		selectItems.add(new SelectItem("Stajyer", "Stajyer"));
		selectItems.add(new SelectItem("Sirket", "Sirket"));
		return selectItems;
	}

	/**
	 * Sisteme girisin uygunlugunu denetler, uygunsa ilgili sayfaya
	 * yonlendirir..
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String login() {
		// girilen userName'e gore veritabanindaki nesneyi getir..
		// intern tablosu icin query..
		Query queryIntern = entityManager
				.createQuery("select i from Intern i where i.userName=:parameter");
		queryIntern.setParameter("parameter", userName);
		// company tablosu icin query..
		Query queryCompany = entityManager
				.createQuery("select c from Company c where c.userName=:param");
		queryCompany.setParameter("param", userName);

		List resultListIntern = queryIntern.getResultList();
		List resultListCompany = queryCompany.getResultList();

		// giris yapan kullanici stajyer ise..
		if (resultListIntern.size() != 0) {
			System.out.println("****** Stajyer sayfasina yonlendiriliyor..");
			// veritabanindan cekilen nesne(intern) sessionda tutulmak icin
			// userData sinifina aktariliyor..
			Intern intern = (Intern) resultListIntern.get(0);
			userData.setIntern(intern);
			userData.setLanguageList(null);
			return "intern";
		} else if (resultListCompany.size() != 0) {
			System.out.println("****** Sirket sayfasina yonlendiriliyor..");
			// veritabanindan cekilen nesne(company) sessionda tutulmak icin
			// userData sinifina aktariliyor..
			userData.setCompany((Company) resultListCompany.get(0));
			return "company";
		} else {
			System.out
					.println("****** Girilen username'e ait kayit bulunamadi..");
		}
		return null;
	}

	/**
	 * Sisteme kayit islemini gerceklestirir..
	 */
	public void userRegister() {
		System.out.println(userName + getPassword() + role
				+ "------------------------------------");

		// kayitlanmak isteyen kullanici tipi sirket ise..
		if (role.equals("Sirket")) {
			// yeni bir sirket yarat..
			Company company = new Company();
			company.setUserName(userName);
			company.setPassword(password);
			company.setRole(role);
			// olusturulan sirketi veritabanina kaydet..
			entityManager.persist(company);
		} else {
			// kayitlanan kullanici bir stajyer..
			Intern intern = new Intern();
			intern.setUserName(userName);
			intern.setPassword(password);
			intern.setRole(role);
			// olusturulan intern'u veritabanina kaydet..
			entityManager.persist(intern);
		}

	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
